<?php


namespace app\community\controller;


use app\common\controller\Together;
use app\common\controller\Common;
use app\index\controller\Base;
use think\Config;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Slide extends Base
{
    //幻灯片列表
    public function index()
    {
        $keyword = '';
        $where = [];

        if (!empty($_GET['keyword'])) {
            $keyword = $_GET['keyword'];
            $where['a.name'] = ['like', "%{$keyword}%"];
        }
        $thisWechat = Session::get("thisWechat");
        $where['b.uniacid'] = $thisWechat['id'];

        if (!empty($_GET['type'])) {
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("community_slide")->alias("a")
                    ->join("community_colony b", "a.colony_id = b.id")
                    ->where($where)->field("a.*,b.name colony_name")->limit($start,$limit)->order("a.sort desc,a.id desc")->select();
                $status = [1 => "启用", 2 => "禁用"];
                $category = Config::get("category");
                foreach ($list as $k => $v) {
                    $list[$k]['category'] = $category[$v['category']];
                    $list[$k]['start_time'] = date("Y-m-d",$v['start_time']);
                    $list[$k]['end_time'] = date("Y-m-d",$v['end_time']);
                    $v['path'] = empty($v['path']) ? "/static/images/nopic-107.png" : $v['path'];
                    $list[$k]['path'] = "<img src='".$v['path']."' style='max-height:50px;max-width:50px;'>";

                }
                $count = Db::name("community_slide")->alias("a")
                    ->join("community_colony b", "a.colony_id = b.id")
                    ->where($where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }


        $assign = [
            'keyword' => $keyword,
        ];
        $this->assign($assign);
        return $this->fetch('index');
    }

    //添加幻灯片视图
    public function addView()
    {

        //获取所管理的景区
        //获取管理的景区
        $Together = new Together();
        $colony = $Together->findColony();

        $audit = Config::get("enable_status");
        $category = Config::get("category");

        $assign = [
            "colony" => $colony,
            "audit" => $audit,
            "category" => $category
        ];
        $this->assign($assign);

        //获取幻灯片详细信息
        if (!empty($_GET['id'])) {
            $id = $_GET['id'];
            $detail = Db::name("community_slide")->where(['id' => $id])->find();
            $this->assign("detail", $detail);
            return $this->fetch('editView');
        } else {
            return $this->fetch('addView');
        }
    }

    //执行添加修改操作
    public function doSlide()
    {
        $common = new Common();
        Db::startTrans();
        try {

            $rule = [
                ["colony_id", "require", "请选择所属景区"],
                ["name", "require", "请输入标题"],
                ["category", "require", "请选择所属类目"],
                ["path", "require", "图片不能为空"],
                ["start_time", "require", "开始时间不能为空"],
                ["end_time", "require", "结束时间不能为空"],
            ];
            $validate = new Validate($rule);
            $value = $this->request->post();
            if (!$validate->check($value)) {
                throw new Exception($validate->getError());
            }

            $value['start_time'] = empty($value['start_time']) ? '' : strtotime($value['start_time']);
            $value['end_time'] = empty($value['end_time']) ? '' : strtotime($value['end_time']);

            if (empty($value['id'])) {
                $value['addtime'] = time();
                $value['add_id'] = Session::get("id");
                $id = Db::name("community_slide")->insertGetId($value);
                $log['remark'] = "用户【" . Session::get("user_name") . "】添加新的幻灯片,幻灯片id【" . $id . "】";
            } else {
                $value['updatetime'] = time();
                $value['update_id'] = Session::get("id");
                $id = $value['id'];
                Db::name("community_slide")->update($value);
                $log['remark'] = "用户【" . Session::get("user_name") . "】更新幻灯片,幻灯片id【" . $id . "】";
            }

            $log['controller'] = "Slide";
            $log['methods'] = "doSlide";
            $common->addLog($log);

            $black['code'] = 0;
            $black['msg'] = "添加成功";
            Db::commit();
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
            Db::rollback();
        }
        return $black;
    }

    //删除
    public function delSlide()
    {
        $common = new Common();
        Db::startTrans();
        try {
            $id = $_POST['id'];
            $navList = Db::name("community_slide")->where(['id' => ['in', $id]])->field("id,name")->select();
            $navList = array_column($navList, "id");
            $navList = implode($navList, ",");


            $log['remark'] = "账号【" . Session::get("user_name") . "】删除幻灯片,删除id【" . $navList . "】";
            $log['controller'] = "Slide";
            $log['methods'] = "delSlide";
            $common->addLog($log);

            Db::name("community_slide")->where(['id' => ['in', $id]])->delete();
            $black['code'] = 0;
            $black['msg'] = "删除成功";
            Db::commit();
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
            Db::rollback();
        }
        return $black;
    }


}